import { ADD_ITEM, DEL_ITEM, INPUT_ITEM, GET_LIST } from "./actionTypes"
const defaultState = {
  inputValue: '',
  placeholder: '做点什么',
  newList: [
    '早睡早起',
    '饮食均衡',
    '把学习玩起来',
    '认识新朋友',
    '总结归纳',
  ]
}
defaultState.newList = defaultState.newList.map(v => {
  return {
    val: v,
    key: new Date().getTime() + Math.random(),
  }
})
export default (state = defaultState, action) => {
  if (action.type === INPUT_ITEM) {
    let newState = JSON.parse(JSON.stringify(state)) //深度拷贝state
    newState.inputValue = action.value
    return newState
  }
  if (action.type === ADD_ITEM) { //根据type值，编写业务逻辑
    let newState = JSON.parse(JSON.stringify(state))
    newState.newList.unshift({
      val: newState.inputValue,
      key: action.key
    })
    newState.inputValue = ''
    return newState
  }
  if (action.type === DEL_ITEM) { //根据type值，编写业务逻辑
    let newState = JSON.parse(JSON.stringify(state))
    newState.newList.splice(action.idx, 1)
    return newState
  }
  if (action.type === GET_LIST){
    // let newState = JSON.parse(JSON.stringify(action.data))
    // return newState
    return action.data
  }
  return state
}
